local function PlayerSay(inst, str)
    if inst.components.talker and not inst:HasTag("mime") then
        inst.components.talker:Say(str)
    end
end

local ACTNAME = L and "modify" or "添加配件"
local NOTHOMURA = L and "I can\'t use that." or "我可操作不了这么精密的仪器!"
local NEEDHOMURA = L and "Homura can manage it." or "这种专业操作还是交给晓美焰来做吧。"
local NOWORKDESK = L and "I need a work desk." or "材料齐全, 现在只缺一个工作台。"
local OCCUPIED = L and "Someone is using the workbench." or "有人正在使用工作台。"


-----加载配件----
local ADD_WEAPON_BUFF = Action({mount_valid = true}) 
ADD_WEAPON_BUFF.id = 'HOMURA_ADDBUFF' ADD_WEAPON_BUFF.str =  ACTNAME
ADD_WEAPON_BUFF.fn = function(act)
    local gun = act.invobject
    local desk = act.target
    local player = act.doer
    if not player:HasTag('homura') then
        PlayerSay(player, NOTHOMURA)
        return true
    end
    if desk and desk.user and desk.user ~= player then
        PlayerSay(player, OCCUPIED)
        return true
    end
    gun.components.container:Open(act.doer)
    gun:PushEvent('homuraevt_openbuffslot',{desk = desk, player = player})
    player:PushEvent('opencontainer', {container = gun})
    return true
end

----假的加载配件---
local Add_BUFF_INV = Action({mount_valid = true})
Add_BUFF_INV.id = 'HOMURA_ADDBUFF_INV' Add_BUFF_INV.str = ADD_WEAPON_BUFF.str
Add_BUFF_INV.fn = function (act)
    if act.target and act.target.components.container:IsOpen() then
        return ACTIONS.STORE.fn(act)
    else
        if act.doer then
            if act.doer:HasTag('homura') then
                local workdesk_free = FindEntity(act.doer, 3, function(inst) 
                    return inst.prefab == 'homura_workdesk' and inst.user == nil 
                end)
                local workdesk_using = FindEntity(act.doer, 3, function(inst)
                    return inst.prefab == 'homura_workdesk' and inst.user
                end)

                if workdesk_using and workdesk_using.user == act.doer then
                    -- 关闭容器
                    if workdesk_using.gun and workdesk_using.gun.components.homura_weapon then
                        workdesk_using.gun.components.homura_weapon:DetachWorkdesk()
                        workdesk_free = workdesk_using
                    end
                end

                if workdesk_free then
                    act.target.components.container:Open(act.doer)
                    act.target:PushEvent('homuraevt_openbuffslot',{desk = workdesk_free, player = act.doer})
                    act.doer:PushEvent('opencontainer', {container = act.target})
                    return ACTIONS.STORE.fn(act)
                elseif workdesk_using then
                    PlayerSay(act.doer, OCCUPIED)
                else
                    PlayerSay(act.doer, NOWORKDESK)
                end
            else
                PlayerSay(act.doer, NEEDHOMURA)
            end
            return true
        end
    end
end

AddAction(ADD_WEAPON_BUFF)
AddStategraphActionHandler("wilson_client", ActionHandler(ADD_WEAPON_BUFF, "give"))
AddStategraphActionHandler("wilson", ActionHandler(ADD_WEAPON_BUFF, "give"))

AddAction(Add_BUFF_INV)
AddStategraphActionHandler("wilson_client", ActionHandler(Add_BUFF_INV, "give"))
AddStategraphActionHandler("wilson", ActionHandler(Add_BUFF_INV, "give"))

local function testfn_useitem(inst, doer, target, actions, right)
    if target.prefab == "homura_workdesk" and doer:HasTag("player") then
        table.insert(actions, ACTIONS.HOMURA_ADDBUFF)
    end
end

local function testfn_equipped(inst, doer, target, actions, right)
    testfn_useitem(inst, doer, target, actions, right)

    if right then
        if inst.prefab == "homura_rifle" and target:HasTag("_combat") and doer:HasTag("player") and not doer:HasTag("homuraTag_issniping") then
            table.insert(actions, ACTIONS.HOMURA_SNIPER)
        end
    end
end

AddComponentAction("USEITEM", "homura_weapon", testfn_useitem)
AddComponentAction("EQUIPPED", "homura_weapon", testfn_equipped)

local testfn = function(inst, doer, target, actions, right)
    if doer:HasTag('player') and (
        target.prefab == 'homura_gun' or
        target.prefab == 'homura_hmg' or
        target.prefab == 'homura_pistol' or
        target.prefab == 'homura_rpg'
        ) then
        table.insert(actions, ACTIONS.HOMURA_ADDBUFF_INV)
    end
end

AddComponentAction("USEITEM", 'homura_weapon_buff', testfn)

----------------
----添加子弹-----
----------------
local ADD_AMMO = Action({mount_valid=true}) 
ADD_AMMO.id = "HOMURA_TAKEAMMO" ADD_AMMO.str = L and "Reload" or "填弹"
ADD_AMMO.fn = function (act)
    act.target.components.homura_weapon:OnAccept(act.invobject, act.doer)
    return true
end
AddAction(ADD_AMMO)
AddStategraphActionHandler("wilson_client", ActionHandler(ADD_AMMO, "give"))
AddStategraphActionHandler("wilson", ActionHandler(ADD_AMMO, "give"))

AddComponentAction("USEITEM", "homura_ammo", function(inst, doer, target, actions, right)
    if target:HasTag("homuraTag_fullammo") then
        return
    end
    if target.prefab == "homura_rpg" then
        if inst:HasTag("homuraTag_rpgammo") then
            table.insert(actions, ACTIONS.HOMURA_TAKEAMMO)
        end
    elseif target:HasTag("homuraTag_gun") then
        if inst.prefab == "homura_gun_ammo1" then
            table.insert(actions, ACTIONS.HOMURA_TAKEAMMO)
        end
    end
end)


-------------
--- 狙击 ----
-------------

local SNIPER = Action({mount_valid=false, distance=math.huge, priority = 100}) 
SNIPER.id = "HOMURA_SNIPER"
SNIPER.str = L and "Snipe" or "开镜"
SNIPER.fn = function(act)
    act.doer.components.homura_sniper:StartSniping()
    return true
end

AddAction(SNIPER)
AddStategraphActionHandler("wilson", ActionHandler(SNIPER, "homura_sniping"))
AddStategraphActionHandler("wilson_client", ActionHandler(SNIPER, function(inst)
    local weapon = inst.replica.combat:GetWeapon()
    if weapon ~= nil and weapon:HasTag("homuraTag_ranged")then
        return "homura_sniping"
    end 
end))

AddComponentAction("POINT", "homura_weapon", function(inst, doer, pos, actions, right)
    if right then
        if inst.prefab == "homura_rifle" then
            if not doer:HasTag("homuraTag_issniping")then
                table.insert(actions, SNIPER)
            end
        end
    end
end)

AddPrefabPostInit("homura_rifle", function(inst)
    inst:AddTag("allow_action_on_impassable")
end)

